#coding=utf-8
import os
from stutils import *
from stvalidator import *
set_django_environ()
from stdj.stock.models import *

start_date = '2007-12-16'
for s in Share.objects.order_by('code'):
    fn = '/home/jay/temp/lastweek/%s.csv'%s.code
    if not os.path.exists(fn):
        continue
    data = csv2data(fn)
    
    print s
     #validation
    for d in data:
        isValidPricefromSina(d)
    
    #data 2 db
    for d in data:
        try:
            pre = s.exday_set.latest('date')
        except Exday.DoesNotExist:
            pre = None
        e = Exday(share=s, date=d[0])   
        if pre: e.pre = pre
        e.save()
        pre = e
        
        v = int(Decimal( d[5] ) * 100 )
        a = int(Decimal( d[6] ) * 10000 )
        
        dd = [todec(i, 2) for i in d[1:5]]
        p = Price(exday=e, start=dd[0], high=dd[1], end=dd[2], low=dd[3], volume=v, amount=a )
        p.save()